package ThreadDemo;

/*
 * Author: JacksonCoder
 * FileName: CompletableFutureCode
 * Version: 1.0.0
 * Date: 2025/2/21
 */

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureCode {

    public static void PrintCompletableFuture() throws ExecutionException, InterruptedException {

        // 创建以恶CompletableFuture并异步执行任务
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello CompletableFuture";
        });

        // 主线程继续执行其他任务
        System.out.println("主线程继续执行其他任务");

        // 获取异步任务的结果 （阻塞直到任务完成
        String result = future.get();
        System.out.println(result);

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        PrintCompletableFuture();
    }

}
